/* 
* @author: keydone
* 2017-02-24
*/
var gulp = require('gulp'),
    rename = require('gulp-rename'),
    header = require('gulp-header'),
    autoprefixer=require('gulp-autoprefixer'),
    spritesmith = require('gulp-spritesmith'),
    minifyCss = require('gulp-clean-css'),
    spriter=require('gulp-css-spriter'),
    concat = require('gulp-concat'),
    uglify = require('gulp-uglify'),
    sass=require('gulp-ruby-sass'),
    watch = require('gulp-watch'),
    shell = require('gulp-shell'),
    util= require( 'gulp-util' ),
    gulpif = require("gulp-if"),
    Jade=require("gulp-jade");

var Version='1.0.0';
var buildDate = util.date(Date.now(), 'isoDate')+" "+util.date(Date.now(), 'isoTime');
var banner = '/*\n * Version:'+Version+' \n * Updated:'+buildDate+'\n*/\n';

var configs={
    distPath: "dist/",
    jsPath: 'public/js/',
    cssPath: 'public/css/',
    sassPath: 'public/sass/'
}

gulp.task('base', function () {
    return gulp.src([
            configs.jsPath+'/lib/common.js'
        ])
        .pipe(concat('base.js'))
        .pipe(gulp.dest(configs.jsPath))
        .pipe(rename({ suffix: '.min' }))
        .pipe(uglify())
        .pipe(header(banner))
        .pipe(gulp.dest(configs.distPath+'/js/'))
});

gulp.task('all', function () {
    return gulp.src([
            configs.jsPath+'/*.js'
        ])
        .pipe(rename({ suffix: '.min' }))
        .pipe(uglify())
        .pipe(header(banner))
        .pipe(gulp.dest(configs.distPath+'/js/'))
});

//编译sass文件
gulp.task('sass', function () {
    return sass(configs.sassPath+"*.scss", {
        style: 'expended'
    }) //expended(展开)  compact(紧凑)  compressed(压缩)  nested(嵌套)
        .pipe(autoprefixer({
            browsers: ['last 20 versions', 'safari 5', 'ios 6', 'android 4']
        }))
        .on('error', function (err) {
            console.error('错误!', err.message);
        })
        /*.pipe(spriter({
           // 生成的spriter的位置
           'spriteSheet': config.distPath+'/images/sprite_bg.png',
           // 生成样式文件图片引用地址的路径 如下将生产：backgound:url(../images/sprite20324232.png)
           'pathToSpriteSheetFromCSS': config.distPath+'/images/sprite_bg.png'
        }))
        .pipe(gulp.dest(config.distPath+"/images/"))*/
        .pipe(gulp.dest(configs.cssPath))
        .pipe(minifyCss({
            advanced: true,
            keepBreaks:false
        }))
        .pipe(rename({ suffix: '.min' }))
        .pipe(header(banner))
        .pipe(gulp.dest(configs.distPath+'css'))

});

// 自动生成雪碧图
gulp.task('sprite', function () {
	return gulp.src('public/slice/*.png')
		.pipe(spritesmith({
			imgName: 'icons_vip.png',
			styleName: 'icons_vip.css',
			imgPath: 'dist/images/icons_vip.png',
			padding: 5
		}))
		.pipe(gulpif('*.png', gulp.dest(configs.distPath+'images')))     //雪碧图输出路径
		.pipe(gulpif('*.css', gulp.dest(configs.cssPath)));    					//css输出路径
});

gulp.task('start', shell.task('npm start'));
gulp.task('dev', shell.task('npm run dev'));
gulp.task('livereload', shell.task('livereload -p public'));

//监听
gulp.task('watch', function () {
    gulp.watch([configs.sassPath+"**/*.scss", 'views/**/*.jade'], ['sass', 'livereload']);
    gulp.watch(configs.jsPath + "**/*.js", ['base', 'all', 'livereload']);
});

gulp.task('default',['watch', 'dev']);
